package net.qhzw.admin.controller;


import net.qhzw.admin.config.JwtTokenUtil;
import net.qhzw.admin.config.JwtUserDetailsService;
import net.qhzw.admin.model.JwtRequest;
import net.qhzw.admin.param.SysUserLoginParam;
import net.qhzw.admin.param.SysUserParam;
import net.qhzw.admin.query.SysUserQuery;
import net.qhzw.admin.service.SysUserService;
import net.qhzw.common.model.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 后台系统用户前端控制器
 */
@RequestMapping("sys/user")
@RestController
public class SysUserController {


    @Autowired
    private SysUserService userService;


    /***
     * 登录以后返回token
     * @param param
     * @return
     */
    @PostMapping("login")
    public JsonResult login(@RequestBody JwtRequest param) {
        return userService.loginAuthentication(param);
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("userInfo")
    public JsonResult userInfo(HttpServletRequest request) {
        return userService.userInfo(request);
    }


    /***
     * 登出功能
     * @return
     */
    @RequestMapping("logout")
    public JsonResult logout() {
        return JsonResult.success(null);
    }


    /***
     * 添加后台管理用户
     * @return
     */
    @PostMapping("add")
    public JsonResult addSysUser(@RequestBody SysUserParam param) {
        return userService.editSysUser(param);
    }

    /***
     * 添加后台管理用户
     * @return
     */
    @PutMapping("modify")
    public JsonResult modifySysUser(@RequestBody SysUserParam param) {
        return userService.editSysUser(param);
    }

    @PutMapping("status")
    public JsonResult setStatus(@RequestBody SysUserParam param) {
        return userService.editSysUser(param);
    }


    /***
     * 删除后台管理用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteSysUser(@PathVariable(value = "id") String id) {
        return userService.deleteSysUser(id);
    }


    @GetMapping("/{id}")
    public JsonResult getSysUser(@PathVariable(value = "id") String id) {
        return userService.getSysUser(id);
    }

    @GetMapping
    public JsonResult getSysUserList(SysUserQuery query) {
        return userService.getSysUserList(query);
    }


    /***
     * 个人信息修改
     * @param param
     * @return
     */
    @PutMapping("updateUserInfo")
    public JsonResult updateUserInfo(@RequestBody SysUserParam param) {
        return userService.updateUserInfo(param);
    }


}
